/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "LUA_GameState.h"

#include "../kernel.h"

extern Kernel* g_kernel;

LUA_GameState::LUA_GameState(void)
{
}

LUA_GameState::~LUA_GameState(void)
{
}

/** Push a state */
int LUA_GameState::PushState( lua_State* L )
{
    std::string state = luaL_checkstring( L, 1 );
    if ( !state.empty() )
    {
        g_kernel->GetGame()->PushState( state );
    }
    return 0;
}
/** Pop a state */
int LUA_GameState::PopState( lua_State* L )
{
    int keepOldActive = luaL_checknumber( L, 1 );
    g_kernel->GetGame()->PopState( keepOldActive );
    
    return 0;
}

/** Get Current State */
int LUA_GameState::GetCurrentState( lua_State* L )
{
    lua_pushstring( L, g_kernel->GetGame()->GetCurrentState()->GetName().c_str() );
    return 1;
}


// -- Bind to LUA
#define method(name) {#name, LUA_GameState::name}

const luaL_reg LUA_GameState::methods[] = {
        method(PushState),
        method(PopState),
        method(GetCurrentState),
        {NULL, NULL}
};

// - Bind to lua
int luaopen_LUA_GameState(lua_State *L) {
    luaL_openlib(L, "gameState", LUA_GameState::methods, 0);
 	return 0;
}